package com.wellsoft.mobileoa.utils.view;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.wellsoft.mobileoa.R;
import com.wellsoft.mobileoa.utils.BaseAction;
import com.wellsoft.mobileoa.utils.BaseActivity;
import com.wellsoft.mobileoa.utils.SystemConfig;
import com.wellsoft.mobileoa.wsmodel.WSOA_Page;
import com.wellsoft.mobileoa.wsmodel.WSOA_PageListItem;
import com.wmobile.utils.ServiceResponse;

import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

public class DataViewAdapter extends BaseAdapter {
	public List<WSOA_PageListItem> listItems;
	public BaseActivity baseActivity;
	public List<Integer> selectedIndex;

	public DataViewAdapter(List<WSOA_PageListItem> listItems,
			BaseActivity baseActivity) {
		this.listItems = listItems;
		this.baseActivity = baseActivity;
		selectedIndex = new ArrayList<Integer>();
	}

	@Override
	public int getCount() {
		if(listItems == null){
			return 0;
		}else{
			return listItems.size();
		}
	}

	@Override
	public Object getItem(int position) {
		return listItems.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		convertView = LayoutInflater.from(baseActivity).inflate(
				R.layout.dataviewitem, null);
		WSOA_PageListItem listItem = listItems.get(position);

		CheckBox dataCb = (CheckBox) convertView
				.findViewById(R.id.dataviewitem_datacb);
		dataCb.setOnClickListener(onCheckBoxSelectListener);
		dataCb.setTag(position);
		if (selectedIndex.contains(position)) {
			dataCb.setChecked(true);
		}

		ImageView dataIv = (ImageView) convertView
				.findViewById(R.id.dataviewitem_dataiv);
		ImageView dataNotReadIv = (ImageView) convertView
				.findViewById(R.id.dataviewitem_datanotreadiv);
		TextView dataTv = (TextView) convertView
				.findViewById(R.id.dataviewitem_datatv);
		TextView dataTv1 = (TextView) convertView
				.findViewById(R.id.dataviewitem_datatv1);

		String temp1 = listItem.getMessage().replace("\r", " ")
				.replace("\n", " ").replace("  ", " ");
		String temp2 = listItem.getTail();
		// if (!SystemConfig.stringIsNullOrEmpty(temp1) && temp1.length() > 18)
		// {
		// temp1 = temp1.substring(0, 18) + "...";
		// }

		int indentify = baseActivity.getResources().getIdentifier(
				baseActivity.getPackageName() + ":drawable/" + "dataview_"
						+ listItems.get(position).getImg(), null, null);
		if (indentify > 0) {
			dataIv.setImageResource(indentify);
			dataNotReadIv.setVisibility(View.GONE);
		} else {
			dataIv.setVisibility(View.GONE);
			dataNotReadIv.setVisibility(View.VISIBLE);
		}
		dataTv.setText(temp1);
		if (!SystemConfig.stringIsNullOrEmpty(temp2)) {
			dataTv1.setText(temp2);
		} else {
			dataTv1.setVisibility(View.GONE);
		}

		if (position == listItems.size() - 1
				&& !baseActivity.getBaseView().isNewPageLoading()
				&& !(baseActivity.getBaseView().getPage().getPageListInfo() == null
						|| baseActivity.getBaseView().getPage()
								.getPageListInfo().getNextPageUrl() == null || baseActivity
						.getBaseView().getPage().getPageListInfo()
						.getNextPageUrl().getAction() == null)) {
			baseActivity.getBaseView().setNewPageLoading(true);
			BaseAction action = BaseAction.createActionFactory(baseActivity
					.getBaseView().getPage().getPageListInfo().getNextPageUrl()
					.getAction(), baseActivity.getBaseView());
			if (action == null) {
				baseActivity.getBaseView().setNewPageLoading(false);
			} else {
				action.setActionNo(1);
				// ServiceResponse response = action.excuteAction();
				// if (response.getExeFlag()) {
				// WSOA_Page nextPage = (WSOA_Page) response
				// .getResponseData();
				// baseActivity.getBaseView().addNextPage(nextPage);
				// }
				// baseActivity.getBaseView().setNewPageLoading(false);
				new AsyncTask<BaseAction, Integer, ServiceResponse>() {

					protected ServiceResponse doInBackground(
							BaseAction... params) {
						BaseAction action = params[0];
						return action.excuteAction();
					}

					@Override
					protected void onPreExecute() {
					}

					@Override
					protected void onPostExecute(ServiceResponse response) {
						if (response.getExeFlag()) {
							WSOA_Page nextPage = (WSOA_Page) response
									.getResponseData();
							baseActivity.getBaseView().addNextPage(nextPage);
						}
						baseActivity.getBaseView().setNewPageLoading(false);
					}

				}.execute(action);
			}
		}

		return convertView;
	}

	public List<String> getCheckedValues() {
		List<String> checkedValues = new ArrayList<String>();
		for (int i = 0; i < selectedIndex.size(); i++) {
			checkedValues.add(listItems.get(selectedIndex.get(i)).getValue());
		}
		return checkedValues;
	}

	public OnClickListener onCheckBoxSelectListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			CheckBox dataCb = (CheckBox) v;
			Integer position = (Integer) dataCb.getTag();
			if (selectedIndex.contains(position)) {
				selectedIndex.remove(position);
			} else {
				selectedIndex.add(position);
			}
			DataViewAdapter.this.notifyDataSetChanged();
		}
	};

	public void selectAll() {
		if (selectedIndex.size() == listItems.size()) {
			selectedIndex.clear();
		} else {
			selectedIndex.clear();
			for (int i = 0; i < listItems.size(); i++) {
				selectedIndex.add(Integer.valueOf(i));
			}
		}
		DataViewAdapter.this.notifyDataSetChanged();
	}

}
